//
//  MYGuidePageManager.m
//  JQLive
//
//  Created by Bruce on 2020/4/8.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "MYGuidePageManager.h"

@interface MYGuidePageManager ()

@property (nonatomic, copy) MYGuidePageCompletionBlock completion;
@property (nonatomic, copy) NSString *guidePageKey;//当前引导页属于哪部分
@property (nonatomic, assign) MYGuidePageType guidePageType;
@end

@implementation MYGuidePageManager

+ (instancetype)defaultSingleton
{
    static MYGuidePageManager *singleton;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[self alloc] init];
    });
    return singleton;
}

- (void)showGuidePageWithType:(MYGuidePageType)type withCoordinatesY:(CGFloat)y
{
    [self creatControlWithType:type withCoordinatesY:y completion:NULL];
}

- (void)showGuidePageWithType:(MYGuidePageType)type withCoordinatesY:(CGFloat)y completion:(MYGuidePageCompletionBlock)completion
{
    [self creatControlWithType:type withCoordinatesY:y completion:completion];
}

- (void)creatControlWithType:(MYGuidePageType)type withCoordinatesY:(CGFloat)y completion:(MYGuidePageCompletionBlock)completion
{
    self.completion = completion;
    
    CGRect frame = [UIScreen mainScreen].bounds;
    UIView *bgView = [[UIView alloc] initWithFrame:frame];
    bgView.backgroundColor = [UIColor colorWithUInt:0x000000 alpha:0.6];
    [bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
    [[UIApplication sharedApplication].keyWindow addSubview:bgView];
    
    //图标，具体步骤显示的图看实际情况
    UIImageView *imgView = [[UIImageView alloc] init];
    [bgView addSubview:imgView];
        
    //使用贝塞尔来挖洞，具体挖的形状看实际需求
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:frame];
    switch (type) {
        case MYGuidePageTypeHome:
        {
            //挖矩形
            [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(5, y, 44, 44) cornerRadius:5] bezierPathByReversingPath]];
            imgView.frame = CGRectMake(20, y+44+20, 40, 40);
            imgView.image = [UIImage imageNamed:@"DigitalAsset_Coin_BTC"];
            [imgView addBouncesAnimation];
        }
        break;
        case MYGuidePageTypeMine:
        {
            //挖圆形，bezierPathWithArcCenter:圆心位置、radius:半径
//            [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH-40, y+22) radius:22 startAngle:0 endAngle:2 * M_PI clockwise:NO]];
            
            [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, y, SCREEN_WIDTH-20, 78) cornerRadius:5] bezierPathByReversingPath]];
            imgView.frame = CGRectMake(70, y+78, 244, 166);
            imgView.image = [UIImage imageNamed:@"ic_guide_page_type_mine1"];
            [imgView addBouncesAnimation];
//            self.guidePageKey = @"MYGuidePageTypeHome";
        }
        break;
        case MYGuidePageTypeMine2:
        {
            [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, y, SCREEN_WIDTH-20, 61) cornerRadius:5] bezierPathByReversingPath]];
            imgView.frame = CGRectMake(70, y-60, 206, 60);
            imgView.image = [UIImage imageNamed:@"ic_guide_page_type_mine2"];
            [imgView addBouncesAnimation];
            self.guidePageKey = MYShowMineFirstTimePage;
        }
        break;
        default:
            break;
    }
    
    //挖洞
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    [bgView.layer setMask:shapeLayer];
}

- (void)tap:(UITapGestureRecognizer *)recognizer
{
    UIView *bgView = recognizer.view;
    [bgView removeFromSuperview];
    [bgView removeGestureRecognizer:recognizer];
    [[bgView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    bgView = nil;
    
    if (self.guidePageKey.length > 0)
    {
        [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:self.guidePageKey];
    }
    
    if (self.completion)
    {
        self.completion();
    }
}

@end
